﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using VOD.Models;
using PagedList;
using System.Data;

namespace VOD.Areas.Admin.Controllers
{
    [Authorize(Roles="Administrators")]
    public class CycledController : Controller
    {
        private VODEntities db = new VODEntities();
        //
        // GET: /Admin/Cycled/
        public ActionResult Index(int? page)
        {
            var videos = db.Videos
                        .Include(v => v.VideoAndTypes.Select(t => t.VideoType))
                        .Include(v => v.VideoLanguage)
                        .Include(v => v.VideoArea)
                        .Where(v => v.Recycled == 1).OrderByDescending(v => v.VideoID);
            //分页，使用PageList
            int pageSize = 20;//定义每页显示的记录数
            int pageNumber = page ?? 1;
            if (pageNumber<1)
            {
                pageNumber = 1;
            }
            return View(videos.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult Recover(int id)
        {
            var videoToRecover = db.Videos.Find(id);
            videoToRecover.Recycled = 0;
            db.Entry(videoToRecover).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // Get: /Admin/Cycled/Delete/5
        public ActionResult Delete(int id)
        {
            try
            {
                var videoToDelete = new Video { VideoID = id };
                db.Entry(videoToDelete).State = EntityState.Deleted;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
